package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.PageResult;
import com.heima.mapper.PolicyMapper;
import com.heima.mapper.VmPolicyMapper;
import com.heima.pojo.Policy;
import com.heima.pojo.VmPolicy;
import com.heima.pojo.dto.PolicyDto;
import com.heima.pojo.dto.PolicyPutDto;
import com.heima.pojo.vo.VmListVo;
import com.heima.service.IPolicyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class PolicyServiceImpl extends ServiceImpl<PolicyMapper, Policy> implements IPolicyService {
    private final PolicyMapper policyMapper;
    private final VmPolicyMapper vmPolicyMapper;

    /**
     * 分页查询
     * @param policyDto
     * @return
     */
    @Override
    public PageResult<Policy> search(PolicyDto policyDto) {
        Page<Policy> page = new Page<>(policyDto.getPageIndex(), policyDto.getPageSize());
        IPage<Policy> pageResult = page(page,Wrappers.<Policy>lambdaQuery().like(StringUtils.hasText(policyDto.getPolicyName()),Policy::getPolicyName,policyDto.getPolicyName()));
        return new PageResult<>(
                policyDto.getPageIndex(),
                policyDto.getPageSize(),
                (int) pageResult.getPages(),
                pageResult.getTotal(),
                pageResult.getRecords()
        );
    }

    /**
     * 修改策略
     * @param policyId
     * @param policyPutDto
     * @return
     */
    @Override
    public Boolean updatePolicy(Integer policyId, PolicyPutDto policyPutDto) {
        // 创建更新条件
        LambdaUpdateWrapper<Policy> wrapper = new LambdaUpdateWrapper<Policy>()
                .eq(Policy::getPolicyId, policyId)
                .set(Policy::getPolicyName, policyPutDto.getPolicyName())
                .set(Policy::getDiscount, policyPutDto.getDiscount());
        // 使用MP根据id修改
        return super.update(wrapper);
    }

    /**
     * 根据vmId获取vm策略
     * @param innerCode
     * @return
     */
    @Override
    public VmPolicy getVmPolicy(String innerCode) {
        return policyMapper.getVmPolicy(innerCode);
    }

    /**
     * 删除策略
     * @param policyId
     * @return
     */
    @Override
    public Boolean removePolicy(Integer policyId) {
        //做删除策略判断
        LambdaQueryWrapper<VmPolicy> wrapper = Wrappers.<VmPolicy>lambdaQuery().eq(VmPolicy::getPolicyId, policyId);
        if (vmPolicyMapper.selectCount(wrapper) > 0) {
            return false;
        }
        // 删除策略
        LambdaQueryWrapper<Policy> policyWrapper = Wrappers.<Policy>lambdaQuery().eq(Policy::getPolicyId, policyId);
        return policyMapper.delete(policyWrapper) > 0;  // 根据 policyId 删除策略
    }

    @Override
    public PageResult<VmListVo> getVmList(Integer policyId, PolicyDto policyDto) {
        IPage<VmListVo> page = new Page<>(policyDto.getPageIndex(), policyDto.getPageSize());
        List<VmListVo> vmList = policyMapper.getVmList(page, policyId);
        return new PageResult<>(
                policyDto.getPageIndex(),
                policyDto.getPageSize(),
                (int) page.getPages(),
                page.getTotal(),
                vmList);
    }

    @Override
    public Boolean savePolicy(PolicyPutDto policyDto) {
        Policy policy = new Policy();
        BeanUtils.copyProperties(policyDto, policy);
        policy.setCreateTime(LocalDateTime.now());
        return super.save(policy);
    }
}
